package com.sdy.healthcode.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.Response;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.dto.Address;
import com.sdy.healthcode.biz.dto.ThisCourtVO;
import com.sdy.healthcode.biz.service.UserService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * @author: 王越洋
 * @version: v1.0
 * @description: com.sdy.healthcode.web.controller
 * @date:2020/3/1
 */

@Slf4j
@Controller
@RequestMapping("/getThisCourt")
@Api(tags = "本院人员管理")
public class ThisCourtController extends BaseController {

    @Autowired
    private UserService userService;

    @GetMapping("/getPage")
    @ResponseBody
    @ApiOperation(value = "本院人员分页")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "姓名", name = "name"),
            @ApiImplicitParam(value = "扫码权限", name = "authScan", allowableValues = "0,1"),
            @ApiImplicitParam(value = "驾驶舱权限", name = "authCockpit", allowableValues = "0,1")
    })
    public Response getPage(String name, Integer authScan, Integer authCockpit, Page<ThisCourtVO> page) throws UnsupportedEncodingException {
        if (StringUtil.isNotBlank(name)) {
            name = URLDecoder.decode(name, StandardCharsets.UTF_8.name());
        }
        IPage<ThisCourtVO> page1 = userService.getPage(name, null, authScan, authCockpit, page);
        for (ThisCourtVO thisCourtVO : page1.getRecords()) {
            Address addr = Address.fromStrToSingleAddr(thisCourtVO.getAddr());
            thisCourtVO.setAddr(addr == null ? "" : addr.getArea() + " " + addr.getAddr());
        }
        return Response.success(page1);
    }

    @PostMapping("/updAuthScan")
    @ResponseBody
    @ApiOperation(value = "启用/取消 扫码权限")
    @ApiImplicitParam(value = "类型0-取消 1-启用", name = "type", allowableValues = "0,1")
    public Response updAuthScan(Integer id, Integer type) {
        if (!type.equals(1) && !type.equals(0)) {
            return Response.error("参数错误");
        }
        userService.updAuthScan(id,type);
        return Response.success();
    }

    @PostMapping("/updAuthCockpit")
    @ResponseBody
    @ApiOperation(value = "启用/取消 驾驶舱权限")
    @ApiImplicitParam(value = "类型0-取消 1-启用", name = "type", allowableValues = "0,1")
    public Response updAuthCockpit(Integer id, Integer type) {
        if (!type.equals(1) && !type.equals(0)) {
            return Response.error("参数错误");
        }
        userService.updAuthCockpit(id, type);
        return Response.success();
    }

}
